﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

namespace EuphoricEngine3D
{
    public class IEServiceContainer : IServiceProvider
    {
        private Dictionary<Type, object> _services = new Dictionary<Type, object>();

        public void AddService(Type Service, object Provider)
        {
            if (_services.ContainsKey(Service))
                throw new Exception("The service container already has a service provider of type " + Service.Name);

            _services.Add(Service, Provider);
        }

        public object GetService(Type Service)
        {
            foreach (Type type in _services.Keys)
                if (type == Service)
                    return _services[type];

            throw new Exception("The service container does not contain a service provider of type " + Service.Name);
        }

        public T GetService<T>()
        {
            object result = GetService(typeof(T));

            if (result != null)
                return (T)result;

            return default(T);
        }

        public void RemoveService(Type Service)
        {
            if (_services.ContainsKey(Service))
                _services.Remove(Service);
        }

        public bool ContainsService(Type Service)
        {
            return _services.ContainsKey(Service);
        }
    }
}